﻿(function ($) {
    document.msCapsLockWarningOff = true;
    var ordersForm = {
        init: function () {
            ordersForm.initControls();
            ordersForm.events.loadData();
            ordersForm.events.loadCabinetType();
            ordersForm.controls.btn_search.on('click',ordersForm.events.loadData);
        },
        initControls: function () {
            ordersForm.controls = {
                dgdetail: $("#dgdetail"),
                search_form: $("#search_form"),
                btn_search: $("#btn_search_ok")
            };
        },
        events: {
            loadData: function () {
                ordersForm.controls.dgdetail.datagrid({
                    sortName: 'Created',
                    idField: 'OrderID',
                    url: '/ashx/ordershandler.ashx?Method=SearchOrders&' + jsNC(),
                    collapsible: false,
                    fitColumns: true,
                    pagination: true,
                    striped: false,   //设置为true将交替显示行背景。
                    pageSize: 20,
                    loadMsg: '正在加载数据，请稍候....',
                    columns: [[
                    {
                        field: 'OrderNo', title: '订单编号', width: 120, align: 'center', formatter: function (value, rowData, rowIndex) {
                            var strReturn = '<a href="javascript:void(0)" class="l-btn-text" title="查看详细" onclick="showdetail(\'' + rowData['OrderID'] + '\',\'' + rowData['OrderNo'] + '\');"><span style="color:#0094ff;">' + rowData['OrderNo'] + '</span></a>';
                            return strReturn;
                        }
                    },
                    { field: 'OutOrderNo', title: '外部单号', width: 105, sortable: false, halign: 'center', align: 'center' },
                    { field: 'PurchaseNo', title: '报价单号', width: 105, sortable: false, halign: 'center', align: 'center' },
                    { field: 'BattchNum', title: '批次号', width: 80, sortable: false, halign: 'center', align: 'center' },
                    { field: 'CabinetType', title: '产品类型', width: 60, sortable: false, align: 'center' },
                    { field: 'OrderType', title: '订单类型', width: 60, sortable: false, align: 'center' },
                    { field: 'CustomerName', title: '客户名称', width: 85, sortable: false, halign: 'center', align: 'center' },
                    { field: 'LinkMan', title: '联系人', width: 70, sortable: false, halign: 'center', align: 'center' },
                    { field: 'Mobile', title: '联系电话', width: 95, sortable: false, align: 'center' },
                    {
                        field: 'BookingDate', title: '订货日期', width: 85, hidden: false, sortable: false, align: 'center', formatter: function (value, rowData, rowIndex) {
                            if (value == undefined) return "";
                            return (new Date(value.replace(/-/g, "/"))).Formats("yyyy-MM-dd");
                        }
                    },
                    {
                        field: 'ShipDate', title: '交货日期', width: 85, sortable: false, align: 'center', formatter: function (value, rowData, rowIndex) {
                            if (value == undefined) return "";
                            return (new Date(value.replace(/-/g, "/"))).Formats("yyyy-MM-dd");
                        }
                    },
                    //{
                    //    field: 'Created', title: '创建时间', width: 120,   sortable: false, align: 'center', formatter: function (value, rowData, rowIndex) {
                    //        if (value == undefined) return "";
                    //        return (new Date(value.replace(/-/g, "/"))).Formats("yyyy-MM-dd HH:mm");
                    //    }
                    //},
                    {
                        field: 'IsStandard', title: '是否正标', width: 60, sortable: false, align: 'center',
                        formatter: function (value, rowData, rowIndex) {
                            if (value == undefined) return "";
                            if (value == "True")
                                return "是";
                            else
                                return "否";
                        }
                    },
                    //{
                    //    field: 'IsOutsourcing', title: '是否外购', width: 60, sortable: false, align: 'center', formatter: function (value, rowData, rowIndex) {
                    //        if (value == undefined) return "";
                    //        if (value == "True")
                    //            return "是";
                    //        else
                    //            return "否";
                    //    }
                    //},

                    {
                        field: 'Status', title: '订单状态', width: 60, align: 'center',
                        formatter: function (value, rowData, rowIndex) {
                            var strReturn = '<a href="javascript:void(0)" class="l-btn-text" title="审核明细" onClick="loadstep(\'' + rowData['OrderID'] + '\');"><span style="color:#0094ff;">' + GetOrderStatusName(rowData['Status']) + '</span></a>';
                            return strReturn;
                        }
                    }
                    ]],
                    onBeforeLoad: function (param) {
                        ordersForm.controls.search_form.find('select').each(function (index) {
                            param[this.name] = $(this).val();
                        });
                        ordersForm.controls.search_form.find('input').each(function (index) {
                            if (this.name != "")
                                param[this.name] = $(this).val();
                        });
                    }
                });
            },
            loadCabinetType: function () {
                $('#CabinetType').combobox({
                    url: '/ashx/categoryhandler.ashx?Method=GetCabinetType',
                    textField: 'CategoryName',
                    valueField: 'CategoryName',
                });
            }
        }
    };
    $(function () {
        ordersForm.init();
    });
})(jQuery);

//加载审核明细
function loadstep(orderid) {
    $('#edit_window').window('open');
    $('#dgsteps').datagrid({
        sortName: 'StepNo',
        idField: 'OrderID',
        url: '/ashx/ordershandler.ashx?Method=GetStepsByOrder&OrderID=' + orderid,
        collapsible: false,
        fitColumns: true,
        pagination: true,
        striped: false,   //设置为true将交替显示行背景。
        pageSize: 20,
        loadMsg: '正在加载数据，请稍候....',
        columns: [[
        { field: 'Action', title: '发起步骤', width: 120, sortable: false, align: 'center' },
        { field: 'TargetStep', title: '目标步骤', width: 150, sortable: false, halign: 'center', align: 'center' },
        { field: 'StartedBy', title: '提交人', width: 150, sortable: false, halign: 'center', align: 'center' },
        {
            field: 'Started', title: '提交时间', width: 150, sortable: false, align: 'center', formatter: function (value, rowData, rowIndex) {
                if (value == undefined)
                    return "";
                var date = new Date(+new Date(value) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
                return date;
            }
        },
        { field: 'EndedBy', title: '处理人', width: 120, sortable: false, align: 'center' },
        {
            field: 'Ended', title: '处理时间', width: 150, sortable: false, align: 'center', formatter: function (value, rowData, rowIndex) {
                if (value == undefined)
                    return "";
                var date = new Date(+new Date(value) + 8 * 3600 * 1000).toISOString().replace(/T/g, ' ').replace(/\.[\d]{3}Z/, '')
                return date;
            }
        },
        { field: 'Remark', title: '审核意见', width: 120, sortable: false, align: 'center' }
        ]],
        onBeforeLoad: function (param) {

        }
    });
    //for (var i = 0; i < 20; i++) {
       // $('#dgsteps').datagrid('reload');
    //}
}
//订单详情
function showdetail(id, no) {
    top.addTab("查看详情-" + no, "/View/crm/ordersdetail.aspx?orderid=" + id + "&" + jsNC(), 'table');
}